<{include file="public/header" /}>

<?php

    use Tools\AdminPermission;

    $role_id = Session::get('role_id');
    $adminPermission = new AdminPermission();
    $adminPermission->setRoleId($role_id);
    $module_name = 'activity_category';
?>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <textarea id="list_filters_box" style="display: none"></textarea>
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">搜索内容</label>
                            <div class="layui-input-inline" style="min-width: 400px">
                                <input type="text" name="filters[search_text]" autocomplete="off" class="layui-input" placeholder="分类名称">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="button" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <{if checkAdminPermissionByAction($adminPermission,$module_name,'create')}>
                    <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <{/if}>
            </div>
        </script>
        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <{if checkAdminPermissionByAction($adminPermission,$module_name,'update')}>
                <a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="update">编辑</a>
            <{/if}>

            <!--<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="delete">删除</a>-->
        </script>

        <script type="text/html" id="statusTextTpl">
            {{#  if(d.status == 1){ }}
            显示
            {{#  }else{ }}
            隐藏
            {{#  } }}
        </script>
    </div>
</div>

<script>
    var $;
    layui.use(['form', 'table', 'upload'], function () {
        $ = layui.jquery;
        var form = layui.form,
            table = layui.table,
            upload = layui.upload;

        table.render({
            elem: '#currentTableId',
            url: "<{:admin_api_url('activity_category/list')}>"
            <{include file="public/table_common" /}>,
            defaultToolbar: ['filter', 'exports', 'print'],
            cols: [[
                //{type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID', sort: true},
                {field: 'title', title: '分类名称'},
                {field: 'describe', title: '分类描述'},
                {field: 'min_price', title: '最小单价'},
                {field: 'min_number', title: '最少数量'},
                {field: 'status', title: '状态', templet:"#statusTextTpl"},
                {field: 'created_at', width: 200, title: '创建时间', sort: true},
                {field: 'tool',title: '操作', minWidth: 200, fixed:'right',templet:'#currentTableBar', align: "center"}
            ]]
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                let url = "<{:admin_api_url('activity_category/create')}>";
                $.post(url,{'_method':'get'},function(res){
                    ajax_response_filtr(res,$adminConfig);
                    if(res.status == 200){
                        var index = layer.open({
                            title: '添加分类',
                            type: 1,
                            shade: 0.2,
                            shadeClose: true,
                            area: ['80%', '80%%'],
                            btn: ['确定', '取消'],
                            content: res.data,
                            success: function(layero, index){

                                $(window).on("resize", function () {
                                    layer.full(index);
                                });

                                //执行实例
                                upload.render({
                                    elem: '.image-btn' //绑定元素
                                    ,url: "<{:admin_api_url('upload/images')}>" //上传接口
                                    ,accept: 'images'
                                    ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                                        layer.load(); //上传loading
                                    }
                                    ,done: function(res){
                                        layer.closeAll('loading');
                                        ajax_response_filtr(res,$adminConfig);
                                        if(res.status == 200){
                                            let data = res.data[0];
                                            $('.image-btn-icon').hide();
                                            $('#icon-image').attr('src',data.url).attr('data-value',data.url).show();
                                            success_msg("上传成功");
                                        }
                                    }
                                    ,error: function(){
                                        layer.closeAll('loading');
                                        error_msg('上传失败');
                                    }
                                });

                                form.render();
                            },
                            yes: function(index, layero){

                                let submitID = 'LAY-role-create-submit';
                                let submit = layero.find('#'+ submitID);

                                //监听提交
                                form.on('submit('+ submitID +')', function(data){

                                    var field = data.field; //获取提交的字段
                                    field.status = field.status == 'on' ? 1 : 2;
                                    field.icon = $('#icon-image').attr('data-value');
                                    if(!field.icon){
                                        error_msg('请分类图标');return false;
                                    }

                                    //提交
                                    $.post(url,field,function(res){
                                        ajax_response_filtr(res,$adminConfig);
                                        if(res.status == 200){
                                            layer.close(index); //关闭弹层
                                            //执行搜索重载
                                            table.reload('currentTableId', {
                                                page: {curr: 1},
                                                where: get_list_filters()
                                            }, 'data');

                                            success_msg('添加成功');
                                        }

                                    },'json')

                                });

                                submit.trigger('click');
                            },
                        });
                    }
                },'json');
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            set_list_filters(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'update') {

                let url = "<{:admin_api_url('activity_category/update')}>";
                $.post(url,{'_method':'get', id: data.id},function(res){
                    ajax_response_filtr(res,$adminConfig);
                    if(res.status == 200){
                        var index = layer.open({
                            title: '分类编辑',
                            type: 1,
                            shade: 0.2,
                            shadeClose: true,
                            area: ['80%', '80%%'],
                            btn: ['确定', '取消'],
                            content: res.data,
                            success: function(layero, index){

                                $(window).on("resize", function () {
                                    layer.full(index);
                                });

                                //执行实例
                                upload.render({
                                    elem: '.image-btn' //绑定元素
                                    ,url: "<{:admin_api_url('upload/images')}>" //上传接口
                                    ,accept: 'images'
                                    ,before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                                        layer.load(); //上传loading
                                    }
                                    ,done: function(res){
                                        layer.closeAll('loading');
                                        ajax_response_filtr(res,$adminConfig);
                                        if(res.status == 200){
                                            let data = res.data[0];
                                            $('.image-btn-icon').hide();
                                            $('#icon-image').attr('src',data.url).attr('data-value',data.url).show();
                                            success_msg("上传成功");
                                        }
                                    }
                                    ,error: function(){
                                        layer.closeAll('loading');
                                        error_msg('上传失败');
                                    }
                                });

                                form.render();
                            },
                            yes: function(index, layero){

                                let submitID = 'LAY-role-update-submit';
                                let submit = layero.find('#'+ submitID);

                                //监听提交
                                form.on('submit('+ submitID +')', function(data){

                                    var field = data.field; //获取提交的字段
                                    field.status = field.status == 'on' ? 1 : 2;
                                    field.icon = $('#icon-image').attr('data-value');
                                    if(!field.icon){
                                        error_msg('请分类图标');return false;
                                    }


                                    //提交
                                    $.post(url,field,function(res){
                                        ajax_response_filtr(res,$adminConfig);
                                        if(res.status == 200){
                                            layer.close(index); //关闭弹层
                                            //执行搜索重载
                                            table.reload('currentTableId', {
                                                page: {curr: 1},
                                                where: get_list_filters()
                                            }, 'data');

                                            success_msg('编辑成功');
                                        }

                                    },'json')

                                });

                                submit.trigger('click');
                            },
                        });
                    }
                },'json');

                return false;
            }else if (obj.event === 'delete') {

                layer.confirm('您真的要删除这个角色吗？', function (index) {
                    layer.close(index);
                    let url = "<{:admin_api_url('activity_category/delete')}>";
                    $.post(url,{id: data.id},function(res){
                        ajax_response_filtr(res,$adminConfig);
                        if(res.status == 200){
                            obj.del();
                            success_msg('操作成功');
                        }
                    },'json');
                });
                return false;
            }
        });

    });
</script>

<{include file="public/footer" /}>